import React from "react";
import {
  Link,
  Routes,
  Route,
  Outlet,
  useParams,
  useRoutes,
  Navigate,
} from "react-router-dom";

const Hot = () => {
  return <div>这是热映页</div>;
};
const Wait = () => {
  return <div>这是待映页</div>;
};
const Home = () => {
  return (
    <>
      <div>这是首页</div>
      <ul>
        <li>
          <Link to="/home/hot">热映</Link>
        </li>
        <li>
          <Link to="/home/wait">待映</Link>
        </li>
      </ul>

      <hr />

      <Outlet />
    </>
  );
};
const About = () => {
  return <div>这是关于页</div>;
};
const Detail = () => {
  const params = useParams();
  return <div>这是详情页 - {params.id}</div>;
};

// 路由表
const routes = [
  {
    path: "/",
    element: <Navigate to="/home"></Navigate>,
  },
  {
    path: "/home",
    element: <Home />,
    children: [
      {
        path: "/home/hot",
        element: <Hot />,
      },
      {
        path: "/home/wait",
        element: <Wait />,
      },
    ],
  },
  {
    path: "/about",
    element: <About />,
  },
  {
    path: "/detail/:id",
    element: <Detail />,
  },
  {
    path: "*",
    element: <div>404</div>,
  },
];

const App = () => {
  const element = useRoutes(routes);

  return (
    <>
      <h2>路由表-v6</h2>
      <ul>
        <li>
          <Link to="/home">首页</Link>
        </li>
        <li>
          <Link to="/about">关于页</Link>
        </li>
        <li>
          <Link to="/detail/234">详情页1</Link>
        </li>
        <li>
          <Link to="/detail/567">详情页2</Link>
        </li>
      </ul>

      <hr />

      {/* 可以把这里的Routes改成路由表的形式 */}
      {/* <Routes>
        <Route path="/home" element={<Home />}>
          <Route path="/home/hot" element={<Hot></Hot>}></Route>
          <Route path="/home/wait" element={<Wait></Wait>}></Route>
        </Route>
        <Route path="/about" element={<About />}></Route>
        <Route path="/detail/:id" element={<Detail />}></Route>
      </Routes> */}

      {element}

      {/* <Routes>
        {routes.map((item) => {
          return <Route path={item.path} element={item.element}></Route>;
        })}
      </Routes> */}
    </>
  );
};

export default App;
